home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / GL / buttonfly / lexer.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  2KB  |  112 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    A very simple lexical analyzer for buttonfly.
  19.  */
  20. #include <stdio.h>
  21. #include "buttonfly.h"
  22. #include "y.tab.h"
  23.  
  24. /*
  25.  *    Externally visible globals, used to communicate between
  26.  * parser/lexer and the main buttonfly.c routines.
  27.  */
  28. FILE *lex_fp;
  29. button_struct *buttons_input;
  30.  
  31. int
  32. yylex()
  33. {
  34.     char *get_line();
  35.     int c;
  36.  
  37.     while (1) {
  38.  
  39.     while ((c = fgetc(lex_fp)) == '\n')
  40.         ;    /* Eat blank lines */
  41.     /* First character on line decides what to do */
  42.     switch(c)
  43.     {
  44.         case EOF:
  45.             return 0;
  46.  
  47.         case '\t':
  48.             return parse_action(get_line(lex_fp));
  49.  
  50.         case '#':
  51.             /* Eat Comments -- free space used in reading in */
  52.             free(get_line(lex_fp));
  53.             break;
  54.  
  55.         default:
  56.             ungetc(c, lex_fp);
  57.             yylval.string = get_line(lex_fp);
  58.             return TITLE;
  59.     }
  60.     }
  61. }
  62.  
  63. /*
  64.  *    mallocs and reads in to end of line, returning resulting string
  65.  */
  66. char *
  67. get_line(fp)
  68. FILE *fp;
  69. {
  70.     char *c;
  71.     int count, in;
  72.  
  73.     count = 1;
  74.     c = malloc(count);
  75.     for (in = fgetc(fp) ; in != EOF && in != '\n' ;
  76.         in = fgetc(fp))
  77.     {
  78.         c[count-1] = (char) in ;
  79.         ++count ;
  80.         c = realloc(c, count);
  81.     }
  82.     c[count-1] = '\0' ;
  83.     return c;
  84. }
  85. /*
  86.  *    Decides if a given string matches a special keyword, or is just a
  87.  * generic action.
  88.  */
  89. int
  90. parse_action(c)
  91. char *c;
  92. {
  93.     int i;
  94.  
  95.     /* Ok, look for special keywords... */
  96.     for (i = 0 ; i < NUM_TOKENS ; i++)
  97.     {
  98.         int ssize ;
  99.         ssize = strlen(dot_tokens[i]) ;
  100.         if (strncmp(c, dot_tokens[i],ssize) == 0)
  101.         {    /* Matched! */
  102.             yylval.string = malloc(strlen(c+ssize) + 1) ;
  103.             strcpy(yylval.string, c+ssize) ;
  104.             free(c) ;
  105.             return token_nums[i];
  106.         }
  107.     }
  108.     /* No match, nothing special, just a generic action */
  109.     yylval.string = c ;
  110.     return ACTION;
  111. }
  112.